class Solution {
public:
    int hammingWeight(int n)
    {
        int a = (n & ((~n) + 1));
        int count = 0;
        while (a > 0)
        {
            count++;
            n = n & (n - 1);
            a = (n & ((~n) + 1));
        }
        return count;
    }

    vector<int> countBits(int n) {
        vector<int> v;
        for (int i = 0; i <= n; i++)
        {
            v.push_back(hammingWeight(i));
        }
        return v;
    }
};